package com.example.java_doc_searcher.searcher;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

public class Parser {
    private static final String INPUT_PATH="E:/doc_searcher_index2/docs/api/";
    private Index index=new Index();
    private AtomicLong t1=new AtomicLong(0);
    private AtomicLong t2=new AtomicLong(0);
    public void run(){
        long beg=System.currentTimeMillis();
        System.out.println("索引制作开始");
        ArrayList<File> fileList=new ArrayList<>();
        enumFile(INPUT_PATH,fileList);
        long endEnumFile=System.currentTimeMillis();
        System.out.println("枚举文件完毕，消耗时间："+(endEnumFile-beg)+"ms");
//        System.out.println(fileList);
//        System.out.println(fileList.size());
        for(File f:fileList){
            System.out.println("开始解析："+f.getAbsolutePath());
            parseHtml(f);
        }
        long endFor=System.currentTimeMillis();
        System.out.println("遍历文件完毕，消耗时间："+(endFor-endEnumFile)+"ms");
        index.save();
        long end=System.currentTimeMillis();
        System.out.println("索引制作完毕，消耗时间:"+(end-beg)+"ms");
    }
    public void runByThread() throws InterruptedException {
        long beg=System.currentTimeMillis();
        System.out.println("索引制作开始");
        ArrayList<File> files=new ArrayList<>();
        enumFile(INPUT_PATH,files);
        CountDownLatch latch=new CountDownLatch(files.size());
        ExecutorService executorService= Executors.newFixedThreadPool(4);
        for(File f:files){
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("解析"+f.getAbsolutePath());
                    parseHtml(f);
                    latch.countDown();
                }
            });
        }
        latch.await();
        executorService.shutdown();
        index.save();
        long end=System.currentTimeMillis();
        System.out.println("索引制作完毕，消耗时间："+(end-beg)+"ms");
        System.out.println("t1: "+t1+",t2: "+t2);
    }

    private void parseHtml(File f) {
        String title=parseTitle(f);
        String url=parseUrl(f);
        long beg=System.nanoTime();
        String content=parseContentByRegex(f);
        long mid=System.nanoTime();
        index.addDoc(title,url,content);
        long end=System.nanoTime();
        t1.addAndGet(mid-beg);
        t2.addAndGet(end-mid);
    }

    public String parseContent(File f) {
        try (BufferedReader bufferedReader=new BufferedReader(new FileReader(f),1024*1024)){
            FileReader fileReader=new FileReader(f);
            boolean isCopy=true;
            StringBuilder content=new StringBuilder();
            while(true){
                int ret=bufferedReader.read();
                if(ret==-1){
                    break;
                }
                char c=(char)ret;
                if(isCopy){
                    if(c=='<'){
                        isCopy=false;
                        continue;
                    }
                    if(c=='\n'||c=='\r'){
                        c=' ';
                    }
                    content.append(c);
                }else{
                    if (c == '>') {
                        isCopy=true;
                    }
                }
            }
            fileReader.close();
            return content.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return"";
    }
    private String readFile(File f){
        try(BufferedReader bufferedReader=new BufferedReader(new FileReader(f))){
            StringBuilder content=new StringBuilder();
            while(true){
                int ret=bufferedReader.read();
                if(ret==-1){
                    break;
                }
                char c=(char)ret;
                if (c == '\n' || c == '\r') {
                    c=' ';
                }
                content.append(c);
            }
            return content.toString();
        }catch (IOException e){
            e.printStackTrace();
        }return "";
    }
    public String parseContentByRegex(File f){
        String content=readFile(f);
        content=content.replaceAll("<script.*?>(.*?)</script>"," ");
        content= content.replaceAll("<.*?>"," ");
        content=content.replaceAll("\\s+"," ");
        return content;
    }

    private String parseUrl(File f) {
        String part1="https://docs.oracle.com/javase/8/docs/api/";
        String part2=f.getAbsolutePath().substring(INPUT_PATH.length());
        return part1+part2;
    }

    private String parseTitle(File f) {
        String name=f.getName();
        return name.substring(0,name.length()-".html".length());
    }

    private void enumFile(String inputPath, ArrayList<File> fileList) {
        File rootPath=new File(inputPath);
        File[] files=rootPath.listFiles();
        for(File f:files){
            if(f.isDirectory()){
                enumFile(f.getAbsolutePath(),fileList);
            }else{
                if(f.getAbsolutePath().endsWith(".html")){
                    fileList.add(f);
                }
            }
        }
    }

    public static void main(String[] args) {
        Parser parser=new Parser();
        parser.run();
    }


}
